package com.vn.egame.elazylite.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.widget.Toast;

import com.vn.egame.elazylite.utinities.FlashTorchUtility;

public class ListenerService extends Service {
	public static final String ACTION_TURNON_FLASH = "com.vn.egame.elazylite.ACTION_TURNON_FLASH";
	public static final String ACTION_TURNOFF_FLASH = "com.vn.egame.elazylite.ACTION_TURNOFF_FLASH";

//	public static final String ACTION_METADATACHANGED = "com.vn.egame.elazylite.ACTION_METADATACHANGED";
//	public static final String EXTRA_METADATA_ARTIST = "com.vn.egame.elazylite.EXTRA_METADATA_ARTIST";
//	public static final String EXTRA_METADATA_TITLE = "com.vn.egame.elazylite.EXTRA_METADATA_TITLE";
//	public static final String EXTRA_PLAYSTATE = "com.vn.egame.elazylite.EXTRA_PLAYSTATE";
//
//	public static final String ACTION_PRE = "com.vn.egame.elazylite.ACTION_PRE";
//	public static final String ACTION_NEXT = "com.vn.egame.elazylite.ACTION_NEXT";
//	public static final String ACTION_PLAY_PAUSE = "com.vn.egame.elazylite.ACTION_PLAY_PAUSE";
//
//	public static final String ACTION_SHOW_GUIDE = "com.vn.egame.elazylite.ACTION_SHOW_GUIDE";
//
//	public static final String ACTION_STOP_SERVICE = "com.vn.egame.elazylite.ACTION_STOP_SERVICE";
//	public static final String ACTION_RESET_SERVICE = "com.vn.egame.elazylite.ACTION_RESET_SERVICE";

	private Context mContext;
	public static boolean isRunning;

	
	
	// public static boolean

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();

		mContext = getApplicationContext();
		isRunning = true;

		IntentFilter filter = new IntentFilter();
		filter.addAction(ACTION_TURNOFF_FLASH);
		filter.addAction(ACTION_TURNON_FLASH);
		registerReceiver(flashStateBroadcastReceiver, filter);
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		return START_STICKY;
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();

		isRunning = false;
		unregisterReceiver(flashStateBroadcastReceiver);
	}

	private BroadcastReceiver flashStateBroadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub

			if (intent == null)
				return;

			if (!FlashTorchUtility.isFlashLightAccessible(mContext)) {

				Toast.makeText(mContext, "Flash not suport", Toast.LENGTH_LONG)
						.show();
				return;
			}
			if (intent.getAction().equals(ACTION_TURNON_FLASH)) {
				FlashTorchUtility.setFlashLightEnabled(mContext, true);
				// isFlashOn = true;
			} else if (intent.getAction().equals(ACTION_TURNOFF_FLASH)) {
				FlashTorchUtility.setFlashLightEnabled(mContext, false);
				// isFlashOn = false;
			}
		}
	};

}
